package mx.kolobok.noteeditor;

import org.apache.log4j.Logger;

import java.io.*;

/**
 * User:  Nikita Belenkiy
 * Date: 24.05.11
 * Time: 20:32
 */
public class FileUtil {
    private static final Logger logger = Logger.getLogger(FileUtil.class);

    private FileUtil() {
    }

    public static void backup(String fileName) throws FileNotFoundException {
        FileInputStream in = new FileInputStream(fileName);
        FileOutputStream out = new FileOutputStream(fileName + ".bak");
        fromInToOut(in, out);
    }

    public static void fromInToOut(InputStream input, OutputStream output) {
        try {
            int bytesRead;
            byte[] buffer = new byte[1024];
            while ((bytesRead = input.read(buffer)) != -1) {
                output.write(buffer, 0, bytesRead);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (input != null) {
                try {
                    input.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (output != null) {
                try {
                    output.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
